import 'package:dio/dio.dart';
import 'package:flutter_tracker/flutter_tracker.dart';
import 'package:uuid/uuid.dart';

class QwsDioInterceptor extends Interceptor {
  var uuid = const Uuid();

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    String traceId = uuid.v4();
    options.headers["traceid"] = traceId;
    FlutterTracker.instance.event(
      eventId: "${options.baseUrl}${options.path}" ,
      eventType: "network-req",
      traceId: traceId,
      extraInfo: options.data.toString(),
    );
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    // TODO: implement onResponse
    String? traceId = response.headers.value("traceid");
    FlutterTracker.instance.event(
      eventId: "${response.requestOptions.baseUrl}${response.requestOptions.path}" ,
      eventType: "network-resp",
      traceId: traceId ?? uuid.v4(),
      extraInfo: response.data.toString(),
    );
    super.onResponse(response, handler);
  }
}
